using System.Diagnostics.Metrics;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.Xml;
using System.Threading;
using System.Text;
using System.IO;

namespace autotest
{
    public partial class FrmTester : Form
    {
        private System.Threading.Timer timer;
        private Button button1;
        private RichTextBox richTextBox1;
        private IOWiFi iowifi;
        private int counter = 0;
        private object locko;

        private string IO_IP = "120.24.226.63";
        int IO_Port = 4627;
        private Button button2;
        private Button button3;
        private bool runtest = false;
        private Label label1;
        private TextBox textBox1;
        private string ipaddress = "192.168.5.100";
        private byte[] data = new byte[1024 * 8];
        private delegate void myDelegate(string str);
        private void InitializeComponent()
        {
            button1 = new Button();
            richTextBox1 = new RichTextBox();
            button3 = new Button();
            label1 = new Label();
            textBox1 = new TextBox();
            SuspendLayout();
            // 
            // button1
            // 
            button1.Location = new Point(852, 103);
            button1.Name = "button1";
            button1.Size = new Size(150, 55);
            button1.TabIndex = 0;
            button1.Text = "START";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            // 
            // richTextBox1
            // 
            richTextBox1.Location = new Point(1, 12);
            richTextBox1.Name = "richTextBox1";
            richTextBox1.Size = new Size(845, 685);
            richTextBox1.TabIndex = 1;
            richTextBox1.Text = "";
            // 
            // button3
            // 
            button3.Location = new Point(852, 178);
            button3.Name = "button3";
            button3.Size = new Size(150, 47);
            button3.TabIndex = 2;
            button3.Text = "Save logs";
            button3.UseVisualStyleBackColor = true;
            button3.Click += button3_Click;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new Point(852, 15);
            label1.Name = "label1";
            label1.Size = new Size(22, 20);
            label1.TabIndex = 3;
            label1.Text = "IP";
            // 
            // textBox1
            // 
            textBox1.Location = new Point(852, 47);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(154, 27);
            textBox1.TabIndex = 4;
            textBox1.Text = "192.168.4.1";
            // 
            // FrmTester
            // 
            ClientSize = new Size(1032, 701);
            Controls.Add(textBox1);
            Controls.Add(label1);
            Controls.Add(button3);
            Controls.Add(richTextBox1);
            Controls.Add(button1);
            Name = "FrmTester";
            ResumeLayout(false);
            PerformLayout();
        }

        public FrmTester()
        {
            InitializeComponent();

        }
        private void setRich(string str)
        {
            if (richTextBox1.InvokeRequired)
            {
                myDelegate md = new myDelegate(this.setRich);
                this.Invoke(md, new object[] { str });
            }
            else
            {
                richTextBox1.AppendText(str);
                richTextBox1.ScrollToCaret();
            }
        }
        private void OnTimedEvent(Object stateInfo)
        {
            // lock (locko) {
            string et;
            this.setRich("enter the function\r\n");

            et = iowifi.Heartbeaton();
            this.setRich(et + "：exit the function\r\n");
            // }   
        }
        public void NTPServerRun()
        {
            IOWiFi iowifi;
            String et;
            iowifi = new IOWiFi();
            ipaddress = textBox1.Text;
            iowifi.init(ipaddress);
            int i = 0;
            String curtime;
            DateTime now;
            while (runtest)
            {

                et = iowifi.Heartbeaton();
                now = DateTime.Now;
                curtime = now.ToString() + "." + now.Millisecond.ToString();
                this.setRich(i.ToString() + "#" + curtime + "#" + et + "\r\n");

                i++;
                //Thread.Sleep(1000);
            }
            // var server = new UdpServer(123);
            // server.Start();
            // Your code here
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //  UdpServer udpserver = new UdpServer(123);
            //    udpserver.Start();


            if (button1.Text != "STOP")
            {
                button1.Text = "STOP";
                runtest = true;
                Thread newThread = new Thread(new ThreadStart(NTPServerRun));
                newThread.Start();


            }
            else
            {
                runtest = false;
                button1.Text = "START";
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string path = "MyTest.txt";



            File.WriteAllText(path, richTextBox1.Text);
        }
    }
}